' ects_setup_sharepoint.vbs
'
' Sets up SharePoint for the External Collaboration Toolkit for SharePoint
'
' Takes seven arguments (see the syntax subroutine)
'
' Return Codes:
'    0 - Successful completion
'    1 - Could not find SharePoint STSADM utility.  Could mean SharePoint is not installed.
'    2 - Could not find the Makecab utility.
'    3 - An expected file is not found in the current directory
'    4 - Could not make the ECTSBase WSP file
'    5 - Could not add WSPs to SharePoint
'    6 - Could not deploy all WSPs
'    7 - Could not activate all features
'    8 - Incorrect arguments provided
'

Set objFSO = Wscript.CreateObject("Scripting.FileSystemObject")
Set objShell = Wscript.CreateObject("Wscript.Shell")
Set colProcessEnvVars = objShell.Environment("Process")

'
' Parse arguments
'

Set args = Wscript.Arguments
If args.Count < 7 Then
    DisplaySyntax
    Wscript.Quit(8)
End If

strLDAPHost = args.Item(0)
strContainer = args.Item(1)
strSQLServer = args.Item(2)
strInternalURL = args.Item(3)
strSMTPHost = args.Item(4)
strMailFrom = args.Item(5)
strLDAPSPort = args.Item(6)

'
' Set up file names
'

strSTSADM = objFSO.BuildPath(colProcessEnvVars("CommonProgramFiles"), "\Microsoft Shared\web server extensions\12\bin\STSADM.exe")
strMakeCab =  objFSO.BuildPath(colProcessEnvVars("windir"), "\system32\makecab.exe")
strFeatureTemplateFile = "ECTSFeatureTemplate.xml"
strFeatureFile = "ECTSFeature.xml"
strManifestFile = "ECTSBaseManifest.xml"
strDDFFile = "ECTSBaseSolution.ddf"
strDLLFile = "EAT.Common.dll"
strBaseWSPFile = "ECTSBase.wsp"
strSolutionWSPFile = "ECTSSolution.wsp"
strUndeploySolutionFile = "undeploysolution.cmd"
strSetupInfFile = "setup.inf"
strSetupRptFile = "setup.rpt"

'
' Ensure that all files are where they need to be
'

if Not objFSO.FileExists(strSTSADM) Then
    Wscript.Echo("Can't find " + strSTSADM + "! Is SharePoint installed?")
    Wscript.Quit(1)
End If

if Not objFSO.FileExists(strMakeCab) Then
    Wscript.Echo("Can't find " + strMakeCab + "!")
    Wscript.Quit(2)
End If

if Not objFSO.FileExists(strFeatureTemplateFile) Then
    Wscript.Echo("Can't find " + strFeatureTemplateFile + ".  Please verify that this file exists")
    Wscript.Echo("in the current directory.")
    Wscript.Quit(3)
End If

if Not objFSO.FileExists(strManifestFile) Then
    Wscript.Echo("Can't find " + strManifestFile + ".  Please verify that this file exists")
    Wscript.Echo("in the current directory.")
    Wscript.Quit(3)
End If

if Not objFSO.FileExists(strDDFFile) Then
    Wscript.Echo("Can't find " + strDDFFile + ".  Please verify that this file exists")
    Wscript.Echo("in the current directory.")
    Wscript.Quit(3)
End If

if Not objFSO.FileExists(strDLLFile) Then
    Wscript.Echo("Can't find " + strDLLFile + ".  Please verify that this file exists")
    Wscript.Echo("in the current directory.")
    Wscript.Quit(3)
End If

if Not objFSO.FileExists(strSolutionWSPFile) Then
    Wscript.Echo("Can't find " + strSolutionWSPFile + ".  Please verify that this file exists")
    Wscript.Echo("in the current directory.")
    Wscript.Quit(3)
End If

if Not objFSO.FileExists(strUndeploySolutionFile) Then
    Wscript.Echo("Can't find " + strUndeploySolutionFile + ".  Please verify that this file exists")
    Wscript.Echo("in the current directory.")
    Wscript.Quit(3)
End If

'
' Echo the parameters
'

Wscript.Echo("Running: cscript ects_setup_sharepoint.vbs " + strLDAPHost + " " + strContainer + " " + strSQLServer + _
    " " + strInternalURL + " " + strSMTPHost + " " + strMailFrom + " " + strLDAPSPort)
Wscript.Echo("")

'
' Clean up any left over stuff in SharePoint from a prior setup run
'

Wscript.Echo("Removing ECTS from SharePoint...")

intReturn = objShell.Run(strUndeploySolutionFile + " " + strInternalURL, 7, True)
Wscript.Echo("")

'
' Create the customized feature file for the ECTSBase.wsp
'

Const FOR_READING = 1
Const FOR_WRITING = 2
iNumberOfLinesToDelete = 1

Set objTS = objFSO.OpenTextFile(strFeatureTemplateFile, FOR_READING)
strContents = objTS.ReadAll
objTS.Close

arrLines = Split(strContents, vbNewLine)

Set objTS = objFSO.CreateTextFile(strFeatureFile)
iIndexToDeleteFrom = UBound(arrLines)- iNumberOfLinesToDelete + 1

For i=0 To UBound(arrLines)
   If i < iIndexToDeleteFrom Then
      objTS.WriteLine arrLines(i)
   End If
Next

objTS.WriteLine "  <Properties>"
objTS.WriteLine "    <Property Key=""LDAPHost"" Value=""" + strLDAPHost + """ />"
objTS.WriteLine "    <Property Key=""LDAPContainer"" Value=""" + strContainer + """ />"
objTS.WriteLine "    <Property Key=""LDAPSPort"" Value=""" + strLDAPSPort + """ />"
objTS.WriteLine "    <Property Key=""SQLServer"" Value=""" + strSQLServer + """ />"
objTS.WriteLine "    <Property Key=""SMTPHost"" Value=""" + strSMTPHost + """ />"
objTS.WriteLine "    <Property Key=""MailFrom"" Value=""" + strMailFrom + """ />"
objTS.WriteLine "  </Properties>"
objTS.WriteLine "</Feature>"
objTS.Close

'
' Create the ECTSBase WSP file
'

Wscript.Echo("Creating the ECTSBase WSP file...")

if objFSO.FileExists(strBaseWSPFile) Then
    objFSO.DeleteFile(strBaseWSPFile)
End If

intReturn = objShell.Run(strMakeCab+ " /f " + strDDFFile, 7, True)

If intReturn = 0 Then
    Wscript.Echo("Done.")
    Wscript.Echo("")
Else
    Wscript.Echo("ERROR: Could not create the ECTSBase WSP file.  Script aborting...")
    Wscript.Echo("")
    Cleanup
    Wscript.Quit(4)
End If

'
' Add the solutions
'

Wscript.Echo("Adding the WSPs to SharePoint...")

intReturn = objShell.Run("""" + strSTSADM + """ -o addsolution -filename " + strBaseWSPFile, 7, True)

If intReturn = 0 Then
    Wscript.Echo("")
    Wscript.Echo(strBaseWSPFile + " added.")
Else
    Wscript.Echo("ERROR: Could not add the " + strBaseWSPFile + " to SharePoint.  Script aborting...")
    Wscript.Echo("")
    Cleanup
    Wscript.Quit(5)
End If

intReturn = objShell.Run("""" + strSTSADM + """ -o addsolution -filename " + strSolutionWSPFile, 7, True)

If intReturn = 0 Then
    Wscript.Echo(strSolutionWSPFile + " added.")
    Wscript.Echo("")
    Wscript.Echo("Done.")
    Wscript.Echo("")
Else
    Wscript.Echo("ERROR: Could not add the " + strSolutionWSPFile + " to SharePoint.  Script aborting...")
    Wscript.Echo("")
    Cleanup
    Wscript.Quit(5)
End If

'
' Deploy the solutions
'

Wscript.Echo("Deploying the solutions to SharePoint...")

intReturn = objShell.Run("""" + strSTSADM + """ -o deploysolution -name " + strBaseWSPFile + " -immediate -force -allowgacdeployment" _
    + " -url " + strInternalURL, 7, True)

If intReturn = 0 Then
    Wscript.Echo("")
    Wscript.Echo(strBaseWSPFile + " deployed.")
Else
    Wscript.Echo("ERROR: Could not deploy " + strBaseWSPFile + ".  Script aborting...")
    Wscript.Echo("")
    Cleanup
    Wscript.Quit(6)
End If

intReturn = objShell.Run("""" + strSTSADM + """ -o deploysolution -name " + strSolutionWSPFile + " -immediate -force -allowgacdeployment" _
    + " -url " + strInternalURL, 7, True)

If intReturn = 0 Then
    Wscript.Echo(strSolutionWSPFile + " deployed.")
    Wscript.Echo("")
Else
    Wscript.Echo("ERROR: Could not deploy " + strBaseWSPFile + ".  Script aborting...")
    Wscript.Echo("")
    Cleanup
    Wscript.Quit(6)
End If

intReturn = objShell.Run("""" + strSTSADM + """ -o execadmsvcjobs", 7, True)
Wscript.Echo("Done.")
Wscript.Echo("")

'
' Activating the features
'

Wscript.Echo("Activating the features...")

intReturn = objShell.Run("""" + strSTSADM + """ -o activatefeature -name ExternalCollaboration -url " + strInternalURL, 7, True)

If intReturn = 0 Then
    Wscript.Echo("")
    Wscript.Echo("ExternalCollaboration feature activated.")
Else
    Wscript.Echo("ERROR: Could not activate ExternalCollaboration.  Script aborting...")
    Wscript.Echo("")
    Cleanup
    Wscript.Quit(7)
End If

intReturn = objShell.Run("""" + strSTSADM + """ -o activatefeature -name ConfigurationUtility -url " + strInternalURL, 7, True)

If intReturn = 0 Then
    Wscript.Echo("ConfigurationUtility feature activated.")
Else
    Wscript.Echo("ERROR: Could not activate ConfigurationUtility.  Script aborting...")
    Wscript.Echo("")
    Cleanup
    Wscript.Quit(7)
End If

intReturn = objShell.Run("""" + strSTSADM + """ -o activatefeature -name SiteCollectionApproval -url " + strInternalURL, 7, True)

If intReturn = 0 Then
    Wscript.Echo("SiteCollectionApproval feature activated.")
Else
    Wscript.Echo("ERROR: Could not activate SiteCollectionApproval.  Script aborting...")
    Wscript.Echo("")
    Cleanup
    Wscript.Quit(7)
End If

intReturn = objShell.Run("""" + strSTSADM + """ -o activatefeature -name SiteCollectionManager -url " + strInternalURL, 7, True)

If intReturn = 0 Then
    Wscript.Echo("SiteCollectionManager feature activated.")
Else
    Wscript.Echo("ERROR: Could not activate SiteCollectionManager.  Script aborting...")
    Wscript.Echo("")
    Cleanup
    Wscript.Quit(7)
End If

intReturn = objShell.Run("""" + strSTSADM + """ -o activatefeature -name UserManager -url " + strInternalURL, 7, True)

If intReturn = 0 Then
    Wscript.Echo("UserManager feature activated.")
Else
    Wscript.Echo("ERROR: Could not activate UserManager.  Script aborting...")
    Wscript.Echo("")
    Cleanup
    Wscript.Quit(7)
End If

intReturn = objShell.Run("""" + strSTSADM + """ -o activatefeature -name CreateSiteCollection -url " + strInternalURL, 7, True)

If intReturn = 0 Then
    Wscript.Echo("CreateSiteCollection feature activated.")
Else
    Wscript.Echo("ERROR: Could not activate CreateSiteCollection.  Script aborting...")
    Wscript.Echo("")
    Cleanup
    Wscript.Quit(7)
End If

intReturn = objShell.Run("""" + strSTSADM + """ -o activatefeature -name ExternalUserApproval -url " + strInternalURL, 7, True)

If intReturn = 0 Then
    Wscript.Echo("ExternalUserApproval feature activated.")
Else
    Wscript.Echo("ERROR: Could not activate ExternalUserApproval.  Script aborting...")
    Wscript.Echo("")
    Cleanup
    Wscript.Quit(7)
End If

intReturn = objShell.Run("""" + strSTSADM + """ -o activatefeature -name ECTSBase -url " + strInternalURL, 7, True)

If intReturn = 0 Then
    Wscript.Echo("ECTSBase feature activated.")
    Wscript.Echo("")
Else
    Wscript.Echo("ERROR: Could not activate ECTSBase.  Script aborting...")
    Wscript.Echo("")
    Cleanup
    Wscript.Quit(7)
End If

intReturn = objShell.Run("""" + strSTSADM + """ -o execadmsvcjobs", 7, True)
Wscript.Echo("Done.")
Wscript.Echo("")

Cleanup
Wscript.Quit(0)

Sub Cleanup

    if objFSO.FileExists(strBaseWSPFile) Then
        objFSO.DeleteFile(strBaseWSPFile)
    End If

    if objFSO.FileExists(strFeatureFile) Then
        objFSO.DeleteFile(strFeatureFile)
    End If

    if objFSO.FileExists(strSetupInfFile) Then
        objFSO.DeleteFile(strSetupInfFile)
    End If

    if objFSO.FileExists(strSetupRptFile) Then
        objFSO.DeleteFile(strSetupRptFile)
    End If
    
End Sub
    
'
' Display the syntax for the script
'

Sub DisplaySyntax
    Wscript.Echo("")
    Wscript.Echo("ects_setup_sharepoint.vbs - Set up SharePoint for the ECTS")
    Wscript.Echo("")
    Wscript.Echo("Usage: cscript ects_setup_sharepoint.vbs LDAPhost container sql_server")
    Wscript.Echo("               internalURL SMTPHost mailfrom LDAPSPort")
    Wscript.Echo("")
    Wscript.Echo("Where: ""LDAPhost""     is the server hosting the ADAM instance,")
    Wscript.Echo("       ""container""    is the base container for the LDAP instance,")
    Wscript.Echo("       ""sql_server""   is the appropriate SQL Server instance,")
    Wscript.Echo("       ""internalURL""  is the URL for the internal SharePoint site,")
    Wscript.Echo("       ""SMTPHost""     is the hostname for the internal SMTP host")
    Wscript.Echo("                      that SharePoint should use")
    Wscript.Echo("       ""mailfrom""     is email address that mail should come from")
    Wscript.Echo("       ""LDAPSport""    is LDAPS port on which ADAM is listening")
    Wscript.Echo("")
    Wscript.Echo("Note:      The script assumes you are running from the directory containing")
    Wscript.Echo("           the various SharePoint files.")
    Wscript.Echo("")
End Sub
